package com.ruoyi.community.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.community.domain.vo.ExerciseVo;
import com.ruoyi.framework.config.ServerConfig;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.community.domain.Exercise;
import com.ruoyi.community.service.IExerciseService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 活动Controller
 *
 * @author lj
 * @date 2022-02-25
 */
@RestController
@RequestMapping("/community/exercise")
public class ExerciseController extends BaseController
{
    @Autowired
    private ServerConfig serverConfig;

    @Autowired
    private IExerciseService exerciseService;

    /**
     * 查询活动列表
     */
    @PreAuthorize("@ss.hasPermi('community:exercise:list')")
    @GetMapping("/list")
    public TableDataInfo list(Exercise exercise)
    {
        startPage();
        List<ExerciseVo> list = exerciseService.selectExerciseLists(exercise);
        return getDataTable(list);
    }

    /**
     * 导出活动列表
     */
    @PreAuthorize("@ss.hasPermi('community:exercise:export')")
    @Log(title = "活动", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Exercise exercise)
    {
        List<Exercise> list = exerciseService.selectExerciseList(exercise);
        ExcelUtil<Exercise> util = new ExcelUtil<Exercise>(Exercise.class);
        util.exportExcel(response, list, "活动数据");
    }

    /**
     * 获取活动详细信息
     */
    @PreAuthorize("@ss.hasPermi('community:exercise:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(exerciseService.selectExerciseById(id));
    }

    /**
     * 新增活动
     */
    @PreAuthorize("@ss.hasPermi('community:exercise:add')")
    @Log(title = "活动", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Exercise exercise)
    {
        String username = SecurityUtils.getUsername();
        exercise.setCreateBy(username);
        return toAjax(exerciseService.insertExercise(exercise));
    }

    /**
     * 修改活动
     */
    @PreAuthorize("@ss.hasPermi('community:exercise:edit')")
    @Log(title = "活动", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Exercise exercise)
    {
        return toAjax(exerciseService.updateExercise(exercise));
    }

    /**
     * 删除活动
     */
    @PreAuthorize("@ss.hasPermi('community:exercise:remove')")
    @Log(title = "活动", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(exerciseService.updateExerciseByIds(ids));
    }
}
